package com.zhuxiang.client;

import com.zhuxiang.model.Student;
import com.zhuxiang.server.ServerDao;
import java.io.IOException;
import java.util.List;

public class ClientStuDao {
    private List<Student> stuList;
    public ClientStuDao(List<Student> stuList){
        this.stuList = stuList;
    }


    /**
     * 判断输入的学员id是否存在
     * @param id
     * @return
     */
    private int isTrue(int id) throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        for (int i = 0; i < stuList.size(); i++) {
            Student stu = stuList.get(i);
            if (id == stu.getId()){
                return i;
            }
        }
        return -1;
    }
    /**
     * 新增学员信息
     */
    public void add() throws IOException, ClassNotFoundException {
        System.out.println("请输入学生学号");
        int id = ClientScanner.getSc().nextInt();
        int aTrue = isTrue(id);
        if (aTrue == -1){
            System.out.println("请输入学生姓名");
            String name = ClientScanner.getSc().next();
            System.out.println("请输入学生密码");
            String pwd = ClientScanner.getSc().next();
            Student stu = new Student(id,name,pwd);
            stuList.add(stu);
            new ServerDao(stuList).writeStu();
            System.out.println("信息添加成功");
        }else {
            System.out.println("该id已存在");
        }

    }

    /**
     * 删除学员信息
     */
    public void delete() throws IOException, ClassNotFoundException {
        System.out.println("请输入删除学生的id");
        int id = ClientScanner.getSc().nextInt();
        int index = isTrue(id);
        if (index != -1){
            stuList.remove(index);
            new ServerDao(stuList).writeStu();
            System.out.println("删除成功");
        }else {
            System.out.println("输入的id有误");
        }
    }
    /**
     * 根据id查找学员信息
     */
    public void findById() throws IOException, ClassNotFoundException {
        System.out.println("请输入学生id");
        int id = ClientScanner.getSc().nextInt();
        int index = isTrue(id);
        if (index != -1){
            System.out.println(stuList.get(index));
        }else {
            System.out.println("输入的id有误");
        }
    }

    /**
     * 更新学员信息
     */
    public void update() throws IOException, ClassNotFoundException {
        System.out.println("请输入学生id");
        int id = ClientScanner.getSc().nextInt();
        int index = isTrue(id);
        if (-1 != index){
            System.out.println("请输入学生姓名");
            String name = ClientScanner.getSc().next();
            System.out.println("请输入学生密码");
            String pwd = ClientScanner.getSc().next();
            Student stu = new Student(id,name,pwd);
            stuList.set(index,stu);
            new ServerDao(stuList).writeStu();
            System.out.println("信息修改成功");
        }else {
            System.out.println("输入的id可能有误");
        }
    }
    /**
     * 查询所有学员信息
     */
    public void findAll() throws IOException, ClassNotFoundException {
        stuList = new ServerDao(stuList).readStu();
        if (0 == stuList.size()){
            System.out.println("尚未录入任何信息");
        }else {
            System.out.println("查询到的信息如下：");
            for (int i = 0; i < stuList.size(); i++) {
                System.out.println(stuList.get(i));
            }
        }
    }

}
